import Mock from 'mockjs'
import { param2Obj } from '@/utils'

const List = []
const count = 100
let maxId = 0

const baseContent = '<p>我是测试数据我是测试数据</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'
const image_uri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'

for (let i = 0; i < count; i++) {
  const m = Mock.mock({
    ID: '@increment',
    UserName: '@first',
    RealName: '@first',
    title: '@title(5, 10)',
    content_short: '我是测试数据',
    content: baseContent,
    forecast: '@float(0, 100, 2, 2)',
    importance: '@integer(1, 3)',
    'type|1': ['CN', 'US', 'JP', 'EU'],
    'status|1': ['published', 'draft', 'deleted'],
    display_time: '@datetime',
    comment_disabled: true,
    pageviews: '@integer(300, 5000)',
    image_uri,
    platforms: ['a-platform']
  })
  maxId = m.ID
  List.push(m)
}

export default {
  getList: config => {
    const { importance, type, title, page = 1, limit = 20, sort, order } = param2Obj(config.url)

    const mockList = List.filter(item => {
      if (importance && item.importance !== +importance) return false
      if (type && item.type !== type) return false
      if (title && item.title.indexOf(title) < 0) return false
      return true
    })

    if (order === 'asc') {
      mockList.sort((a, b) => {
        if (a[sort] < b[sort]) {
          return -1
        }
        if (a[sort] > b[sort]) {
          return 1
        }
        return 0
      })
    } else if (order === 'desc') {
      mockList.sort((a, b) => {
        if (a[sort] > b[sort]) {
          return -1
        }
        if (a[sort] < b[sort]) {
          return 1
        }
        return 0
      })
    }

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

    return {
      success: true,
      data: {
        total: mockList.length,
        items: pageList
      }
    }
  },
  getPv: () => ({
    pvData: [{ key: 'PC', pv: 1024 }, { key: 'mobile', pv: 1024 }, { key: 'ios', pv: 1024 }, { key: 'android', pv: 1024 }]
  }),
  getArticle: (config) => {
    const { id } = param2Obj(config.url)
    for (const article of List) {
      if (article.id === +id) {
        return {
          success: true,
          data: {
            article
          }
        }
      }
    }
  },
  add: (config) => {
    const data = JSON.parse(config.body)
    maxId += 1
    data.ID = maxId
    List.push(data)
    return {
      success: true,
      data: data
    }
  },
  update: (config) => {
    const data = JSON.parse(config.body)
    for (const m of List) {
      if (m.ID === data.ID) {
        const index = List.indexOf(m)
        List.splice(index, 1, data)
        return {
          success: true,
          data: data
        }
      }
    }
  },
  remove: (config) => {
    // console.log(config)
    const urlArray = config.url.split('/')
    const id = urlArray[urlArray.length - 1]
    for (const m of List) {
      if (m.ID === id) {
        const index = List.indexOf(m)
        List.splice(index, 1)
        return {
          success: true
        }
      }
    }
    return {
      success: true
    }
  }
}
